/**权限视图
 * by Steve Jin
 */
//模型文件版本数据
var versioninfoStore = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({	//proxy从哪里获取数据
		url : 'showSearchFileList.action?method=showSearchFileList&keyWord=',
		method : 'post'
	}),
	reader : new Ext.data.JsonReader({	//reader如何解析数据
		root : 'File',
		totalProperty : 'totalFile',
		id : 'id'
	}, [ {
		name : 'id'
	}, {
		name : 'name'
	},{
		name : 'version'
	}, {
		name : 'xmlpath'
	}, {
		name : 'company'
	}, {
		name : 'type'
	} ])
});
//版本表格列模型
var versioninfosm=new Ext.grid.CheckboxSelectionModel ({singleSelect : false});
var versioninfoColumnModel=new Ext.grid.ColumnModel(
		[versioninfosm,
		 new Ext.grid.RowNumberer(),
		 {
			header : '文件名',
			width : 160,
			dataIndex : 'name',
			sortable : true,
			id : 'name'
		 },   {
			 header : '版本',
			 width : 130,
			 dataIndex : 'version',
			 sortable : true,
             align : 'center'
		 },{
			 header : '类型',
			 width : 60,
			 dataIndex : 'type',
			 sortable : true,
			 align : 'center',
			 renderer:function(value, cellmeta, record){
			 var typemap=[['1','流程模型']
			               ,['2','功能模型']
                           ,['3','组织模型']
                           ,['4','业务对象模型']
                           ,['5','epc模型']
                           ,['6','权限模型']
                           ,['7','角色模型']];
                        	return typemap[value-1][1];
                            }
         }]
          );
//模型版本信息列表
var versioninfoList=new Ext.grid.EditorGridPanel({
	title: "版本历史",
	region:'center',
	ds : versioninfoStore,	//绑定store
	enableColumnResize : true,
	border : true,
	//clicksToEdit:1,
	enableColumnMove : true,
	enableHdMenu : false,
	stripeRows: true, //显示斑马线
	loadMask: true,
	autoHeight:true,
	bodyStyle:'width:100%',
    autoWidth:true,
    editable:true,
    sm : versioninfosm,
    bbar: new Ext.PagingToolbar({//下面的分页条
        pageSize: 15,
        store: versioninfoStore,
        displayInfo: true,
        displayMsg: '当前显示第 {0} 条到 {1} 条记录，一共 {2} 条',
        emptyMsg: "没有记录",
        lastText:"尾页",   
        nextText :"下一页",   
        prevText :"上一页",   
        firstText :"首页",   
        refreshText:"刷新页面"        
    }),
	cm : versioninfoColumnModel,
	store : versioninfoStore
});
var versioninfoTabPanel = new Ext.TabPanel({
	border : true,
	region:'south',
	autoScroll:true,
	//clicksToEdit:1,
	//autoHeight:true,
    //autoWidth:true,
	height:200,
	items:[ versioninfoList,
	{
	title: "权限资源信息",
	region:'center',
	
	collapsible : true,
	border:false,
	contentEl:'permissionReresourcesDetail'
	}]
	,
	activeItem: 1
	});

var role = new Ext.Panel({
	id : 'perm-panel',
	title : '权限视图',
	layout : 'border',
	defaults : {
		
		bodyStyle : 'padding:15px'
	},
	items : [  versioninfoTabPanel,
	           {
		region : 'center',
		border : false,
		autoScroll:true,
		contentEl : 'permCanvas'
	} ]
	/*items : [  {
		tbar: [{xtype:'button',width:50,text:'返回',iconCls: 'return',handler:returnToRoleList}],
		region : 'center',
		border : false,
		autoScroll:true,
		contentEl : 'permCanvas'
	} ]*/
});

function returnToRoleList(){
	
	  Ext.getCmp('content-panel').layout.setActiveItem('role-list-panel');
	  showRoleList();
}

var permissionresourcesStore = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : 'showPermissionresources.action?method=showPermissionresources',
		method : 'post'
	}),
	reader : new Ext.data.JsonReader({
		root : "Permissionresources",
		totalProperty : "totalPermissionresources",
		id:"id"
    }, [ {
		name : 'id'
	}, {
		name : 'resourceid' 
	},{
		name : 'permissionid'
	}])
});  

var resourceStore;
var rAmount;


function showPermissionContent(record2) {
	
    var cid=record2.get('id');
    //cid=permid;
    permissionresourcesStore.load({ 
    	params : {
			cid : cid
		},
		callback: function(r, options, success) { 
		if (success == false) { 
		alert("加载perm-res数据失败，无对应数据或者系统出现异常！"); 
		}
		else{
			resourceStore = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
					url : 'showResource.action?method=showResource',
					method : 'post'
				}),
				reader : new Ext.data.JsonReader({
					root : "resource",
					totalProperty : "totalResource",
					id:"id"
			    }, [ {
					name : 'id'
				}, {
					name : 'enName'
				},{
					name : 'cnName'
				},{
					name : 'description'
				},{
					name : 'serviceName'
				},{
					name : 'methodName'
				},{
					name : 'functionId'
				},{
					name : 'fileId'
				},{
					name : 'groupId'
				},{
					name : 'version'
				},{
					name : 'type'
				}])
			});
		resourceStore.load({
			params : {
				cid : cid
			},
			callback: function(r,options,success){
				if (success == false){
					//alert("false:"+resourceStore.getCount());
					alert("加载resource数据失败，无对应数据或者系统出现异常！");
				}
				else{
					//alert("true:"+resourceStore.getCount());
				var canvas=document.getElementById('permCanvas');
				var ctx=canvas.getContext('2d');
				 	drawPermModel(350,200,ctx,permissionresourcesStore,resourceStore,record2,canvas);
					 Ext.getDom('permissionReresourcesDetail').innerHTML = "";
					 var cm=new Ext.grid.ColumnModel([
					              					new Ext.grid.RowNumberer(),
					              					{header:'编号',dataIndex:'id'},
					              					{header:'名称(中文)',dataIndex:'cnName'},
					              					{header:'名称(英文)',dataIndex:'enName'},
					              					{header:'备注',dataIndex:'description'},
					              					{header:'服务名',dataIndex:'serviceName'},
					              					{header:'方法名',dataIndex:'methodName'},
					              					{header:'功能点',dataIndex:'functionId'},
					              					{header:'资源类型',dataIndex:'type'},
					              				]);
					              			var grid=new Ext.grid.GridPanel({
					              					renderTo:'permissionReresourcesDetail',
					              					 viewConfig: {
					              			            forceFit:true
					              			        },
					              					layout:'fit',
					              					stripeRows: true,
					              					autoHeight:true,
					              					loadMask:true,
					              					store: resourceStore,
					              					cm:cm
					              					,			              						
					              					listeners : {
					              						cellclick : {
					              						fn : function(grid, rowIndex, columnIndex, e) {
					              							//点击记录，在表单中显示记录信息
					              						selected_row = rowIndex;
					              						var record = grid.getStore().getAt(rowIndex);
					              						//var fAmount=grid.getCount();
					              						var functionpointStore = new Ext.data.Store({
					              							proxy : new Ext.data.HttpProxy({
					              								url : 'showFunctionPoint.action?method=showFunctionPoint',
					              								method : 'post'
					              							}),
					              							reader : new Ext.data.JsonReader({
					              								root : "functionpoint",
					              								totalProperty : "totalFunctionpoint",
					              								id:"id"
					              						    }, [ {
					              								name : 'id'
					              							}, {
					              								name : 'fileId'
					              							}, {
					              								name : 'enName'
					              							},{
					              								name : 'cnName'
					              							},{
					              								name : 'description'
					              							},{
					              								name : 'groupId'
					              							},{
					              								name : 'versionId'
					              							}])
					              						});
					              			    		var cid = resourceStore.getAt(rowIndex).get('functionId');
					              			    		//alert("funid:"+cid);
					              						functionpointStore.load({
					              			    			params : {
					              			    				cid : cid
					              			    			},
					              			    			callback: function(r,options,success){
					              			    				if (success == false){
					              			    					//alert("false:"+resourceStore.getCount());
					              			    					alert("加载functionpoint数据失败，无对应数据或者系统出现异常！");
					              			    				}
					              			    				else{
					              			    					var fid = functionpointStore.getAt(0).get('id');
					              			    					var fText = functionpointStore.getAt(0).get('cnName');
					              			    			 	    Ext.getCmp('content-panel').addTab('functionpoint-panel');
					              			    			 	    showFunctionContent(fid,fText,'-1','-1');
					              			    				}
					              			    			}
					              			    		});
					              						
					              						
//					              						Ext.getCmp('content-panel').addTab('functionpoint-panel');
//					              				      	showFunctionContent(functionStore.getAt(rowIndex).get('id'),functionStore.getAt(rowIndex).get('cnName'),processid,pText);					    
					              						},
					              						scope : this
					              					}
					              				}
					              				});
				}
			}
		});
			
			
			}
		}
		});
   }





function drawPermModel(startx,starty,ctx,permissionresourcesStore,resourceStore,record2,canvas){
	 ctx.restore();
	 ctx.clearRect(0, 0, 1500, 1500);
	 ctx.save();
	 ctx.translate(startx, starty);
	 
	 var prAmount=permissionresourcesStore.getCount();
	 //alert(prAmount);
	 rAmount=resourceStore.getCount();
	 //alert(rAmount);

	 var permattr=new Array();

	 var permlength;
	 var reslength; 

	 permattr[0] = record2.get('id');
	 permattr[1] = record2.get('enName');
	 permattr[2] = record2.get('cnName');
	 permattr[3] = record2.get('description');
	 permattr[4] = record2.get('permissionPackage');
	 permattr[5] = record2.get('fileId');
	 permattr[6] = record2.get('groupId');
	 permattr[7] = record2.get('versionId');
	 
	 var length=(permattr.length+1)*20; 
	 var gradient = ctx.createLinearGradient(0, 0, 0, length);
	 gradient.addColorStop(0, 'rgb(205,205,205)');
	 gradient.addColorStop(1, 'rgb(255,255,255)');
	 ctx.fillStyle = gradient;
	 ctx.fillRect( 0, 0, 265, length); //渐变的背景色
	 
	 ctx.strokeStyle = '#000000'; 
	 ctx.beginPath(); 
	 ctx.rect(0.5,0.5, 265, length);
	 ctx.stroke();              //黑边框
	 
	 var permImg=new Image();
	 permImg.onload=function(){
	 ctx.drawImage(permImg,3,2,16,16);
     };
	 permImg.src='images/modelUnit/department/perm.png';
	 
	 ctx.fillStyle ="black";
	 ctx.font = "10pt 宋体";
	 ctx.fillText('权限:'+record2.get('cnName'),25,16,265); // 标题栏
	 
	 ctx.beginPath();
	 ctx.moveTo(0,20);
	 ctx.lineTo(265,20);
	 ctx.stroke();   
	 ctx.beginPath();
	 ctx.moveTo(0.5,20.5);
	 ctx.lineTo(265.5,20.5);
	 ctx.stroke();        //画分割线
	 
	
	 
	 var permattrImg=new Image();        //填写属性列表
	 permattrImg.onload=function(){
		 
	 for (var i=0;i<permattr.length-1;i++){
		
		 ctx.drawImage(permattrImg,3,20+20*i,16,16);
	 };
		 
		 
	 	 
	};
	 permattrImg.src='images/modelUnit/function/parameter_info.gif';
	 
	 ctx.fillStyle ="black";
	 ctx.font = "8pt Arial";
     ctx.fillText('编号:'+permattr[0],22,36);
	 ctx.fillText('英文名:'+permattr[1],22,56,265);
	 // ctx.fillText('详细信息:'+permattr[3],22,76,265);
	 ctx.fillText('权限包:'+permattr[4],22,76,265);
	 ctx.fillText('索引:'+permattr[5],22,96,265);
	 ctx.fillText('模型库:'+permattr[6],22,116,265);
	 ctx.fillText('版本信息:'+permattr[7],22,136,265);
	 ctx.fillText('详细信息:'+permattr[3],22,156,265);;//属性栏
	 
	 
	 ctx.beginPath();
	 ctx.lineWidth = 1;
	 ctx.moveTo(0,20+20*permattr.length);
	 ctx.lineTo(265,20+20*permattr.length);
	 ctx.stroke();   
	 ctx.beginPath();
	 ctx.moveTo(0.5,20+20*permattr.length+0.5);
	 ctx.lineTo(265.5,20+20*permattr.length+0.5);
	 ctx.stroke();  //画分割线
	 
/*	 var permresImg=new Image();         // 填写权限列表
     permresImg.onload=function(){ 
     for (var j=0;j<prAmount;j++){
		 ctx.drawImage(permresImg,3,(1+permattr.length+j)*20,16,16);
		 ctx.fillStyle ="black";
		  var res=resourceStore.getAt(j);
		 ctx.font = "10pt Arial";
		 ctx.fillText('资源'+(j+1)+': '+res.get('cnName'),22,+(1+permattr.length+j)*20+16,265); // 属性栏
	 }
     };
     permresImg.src='images/modelUnit/domainModel/state.gif';
*/	     
	 
		 var resImg=new Image();
		 resImg.onload=function(){
		var resattrImg=new Image();
	  		resattrImg.onload=function(){
	 for (var m=0;m<rAmount;m++)
	 {   var resattr=new Array();
		 resattr[0] = resourceStore.getAt(m).get('id');
		 resattr[1] = resourceStore.getAt(m).get('enName');
		 resattr[2] = resourceStore.getAt(m).get('cnName');
		 resattr[3] = resourceStore.getAt(m).get('description');
		 resattr[4] = resourceStore.getAt(m).get('serviceName');
		 resattr[5] = resourceStore.getAt(m).get('methodName');
		 resattr[6] = resourceStore.getAt(m).get('functionId');
		 resattr[7] = resourceStore.getAt(m).get('fileId');	
		 //alert(resattr[7]);
		 resattr[8] = resourceStore.getAt(m).get('groupId');	
		 resattr[9] = resourceStore.getAt(m).get('version');
		 resattr[10] = resourceStore.getAt(m).get('type');
		 reslength = (resattr.length)*20; 
		 var x=parseInt(-280*Math.cos(m*Math.PI*2/rAmount));
		     if (x>265) x=1.2*x;
		     var y=parseInt(-280*Math.sin(m*Math.PI*2/rAmount));
		     gradient = ctx.createLinearGradient(x, y, x, reslength);
		     gradient.addColorStop(0, 'rgb(255,255,146)');
			 gradient.addColorStop(1, 'rgb(255,255,231)');
			 ctx.fillStyle = gradient;
			 ctx.fillRect( x, y, 265, reslength); // 渐变的背景色
			
			 ctx.lineWidth = 1;
			 ctx.strokeStyle = '#000000'; 
			 ctx.beginPath(); 
			
			 ctx.rect(x-0.5,y-0.5, 265, reslength);
			 ctx.stroke();              // 黑边框
			 
			 
			
			 ctx.drawImage(resImg,x+3,y+2,16,16);
			
			 ctx.fillStyle ="black";
			 ctx.font = "10pt Arial";
			 ctx.fillText('资源:'+resattr[2],x+25,y+16,265); // 标题栏
			 
			 ctx.beginPath();
			 ctx.moveTo(x,y+20);
			 ctx.lineTo(x+265,y+20);
			 ctx.stroke();
			 
			 ctx.beginPath();
			 ctx.moveTo(x+0.5,y+20.5);
			 ctx.lineTo(x+265,y+20.5);
			 ctx.stroke();        // 画分割线
			 
			
				 for (var l=0;l<resattr.length-1;l++){
					 ctx.drawImage(resattrImg,x+3,y+22+l*20,16,16);
				 }
			
		 ctx.fillStyle ="black";
			 ctx.font = "8pt Arial";
	     ctx.fillText('编号:'+resattr[0],x+22,y+36);
		 ctx.fillText('英文名:'+resattr[1],x+22,y+56,265);
		 ctx.fillText('操作:'+resattr[3],x+22,y+76,265);
		 ctx.fillText('服务名:'+resattr[4],x+22,y+96,265);
		 ctx.fillText('方法名:'+resattr[5],x+22,y+116,265);
		 ctx.fillText('功能点:'+resattr[6],x+22,y+136,265);
		 ctx.fillText('索引:'+resattr[7],x+22,y+156,265);
		 ctx.fillText('模型库:'+resattr[8],x+22,y+176,265);
		 ctx.fillText('版本号:'+resattr[9],x+22,y+196,265);
		 ctx.fillText('资源类型:'+resattr[10],x+22,y+216,265);
		 drawLine(x+132.5,y+110,132.5,length/2,ctx,265,220);
	 }
	  		 };
	
		resattrImg.src='images/modelUnit/function/parameter_info.gif';
		canvas.onclick = onResourceMouseClick;
	};
		resImg.src='images/modelUnit/department/resource.png';
			 
	
	}

function onResourceMouseClick(event) {
	var functionpointStore = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : 'showFunctionPoint.action?method=showFunctionPoint',
			method : 'post'
		}),
		reader : new Ext.data.JsonReader({
			root : "functionpoint",
			totalProperty : "totalFunctionpoint",
			id:"id"
	    }, [ {
			name : 'id'
		}, {
			name : 'fileId'
		}, {
			name : 'enName'
		},{
			name : 'cnName'
		},{
			name : 'description'
		},{
			name : 'groupId'
		},{
			name : 'versionId'
		}])
	});
	 var domainmodelStore = new Ext.data.Store({
			proxy : new Ext.data.HttpProxy({
				url : 'showDomainModel.action?method=showDomainModel',
				method : 'post'
				
			}),
		reader : new Ext.data.JsonReader({
	root : "DomainModel",
	totalProperty : "totalDomainModel",
	id : "id"
		}, [ {
			name : 'id'
		}, {
			name : 'enName'
		}, {
			name : 'cnName'
		}, {
			name : 'description'
		}, {
			name : 'type'
		}, {
			name : 'category'
		}, {
			name : 'fileId'
		}] )
});

	 var i=0;
	 var x=event.layerX-350;
	 var y=event.layerY-200;
	 if (x/1.2>265) x=x/1.2;
	 while (i<rAmount){
     
     if (x>-280*Math.cos(i*Math.PI*2/rAmount)
    	 && x<-280*Math.cos(i*Math.PI*2/rAmount)+265
         && y>-280*Math.sin(i*Math.PI*2/rAmount)
         && y<-280*Math.sin(i*Math.PI*2/rAmount)+200)
    {
    		var cid = resourceStore.getAt(i).get('functionId');
    		var type = resourceStore.getAt(i).get('type');
    		//alert("funid:"+cid);
			if (type == 'ResourceFunction'){
				functionpointStore.load({
	    			params : {
	    				cid : cid
	    			},
	    			callback: function(r,options,success){
	    				if (success == false){
	    					//alert("false:"+resourceStore.getCount());
	    					alert("加载functionpoint数据失败，无对应数据或者系统出现异常！");
	    				}
	    				else{
	    					//alert('funStore:'+functionStore.getCount());
	    					var fid = functionpointStore.getAt(0).get('id');
	    					var fText = functionpointStore.getAt(0).get('cnName');
	    			 	    Ext.getCmp('content-panel').addTab('functionpoint-panel');
	    			 	    showFunctionContent(fid,fText,'-1','-1');
	    				}
	    			}
	    		});
			}
			else if (type == 'ResourceDomain'){
				domainmodelStore.load({
					params : {
						cid : cid
					},
						callback: function(r, options, success) { 
	                       if (success == false) {  
	                           alert("加载数据失败，无对应数据或者系统出现异常！"); 
	                       }else{ 
	                    	   var epcRecord4=domainmodelStore.getAt(0);
	                    	   showModelContent(epcRecord4.get('id'),epcRecord4.get('cnName'));
	                   		  
	                   		   Ext.getCmp('content-panel').addTab('domain-panel');	
		
		              						
		              					}
		              				
		              				}
	    		});
			}


	       break;
     }
      i++;
	 }
} 



